﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data;

namespace cau2
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        sinhvien a = new sinhvien();
        public MainWindow()
        {
            InitializeComponent();
        }
        private void btnthoat_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            DataTable dt = a.hienthi();
            datagrid.ItemsSource = dt.DefaultView;
            datagrid.SelectedValuePath = "masv";
        }

        private void btnthem_Click(object sender, RoutedEventArgs e)
        {
            txtma.Text = "";
            txtho.Text = "";
            txtten.Text = "";
            txtgioitinh.Text = "";
            txtkhoa.Text = "";
            datePicker1.Text = "";
            btnluu.IsEnabled = true;
            btnhuy.IsEnabled = true;

        }

        private void button4_Click(object sender, RoutedEventArgs e)
        {
            String ma = txtma.Text;
            String ho = txtho.Text;
            String ten = txtten.Text;
            String sex = txtgioitinh.Text;
            String makhoa = txtkhoa.Text;
            String ngay = datePicker1.Text;
            DataView dv = (DataView)datagrid.ItemsSource;
            DataRowView row = dv.AddNew();
            a.Them(ma, ho, ten, ngay, sex, makhoa);
            row["masv"] = ma;
            row["hosv"] = ho;
            row["tensv"] = ten;
            row["ngaysinh"] = ngay;
            row["gioitinh"] = sex;
            row["makhoa"] = makhoa;
            row.EndEdit();
            datagrid.Items.Refresh();

        }

        private void button5_Click(object sender, RoutedEventArgs e)
        {
            txtma.Text = "";
            txtho.Text = "";
            txtten.Text = "";
            txtgioitinh.Text = "";
            txtkhoa.Text = "";
            datePicker1.Text = "";
            btnluu.IsEnabled = false;
            btnhuy.IsEnabled = false;
        }

        private void btnxoa_Click(object sender, RoutedEventArgs e)
        {
            String masv = datagrid.SelectedValue.ToString();

            MessageBoxResult res = MessageBox.Show("ban co muon xoa sinhvien " + masv + " khong ?", "thông báo", MessageBoxButton.YesNo, MessageBoxImage.Warning);
            if (res == MessageBoxResult.Yes)
            {
                a.Xoa(masv);
                DataRowView rowselect = (DataRowView)datagrid.SelectedItem;
                rowselect.Delete();
            }
        }

        private void btnsua_Click(object sender, RoutedEventArgs e)
        {
            DataRowView dv = (DataRowView)datagrid.SelectedItem;
            txtma.Text = dv["masv"].ToString();
            txtho.Text = dv["hosv"].ToString();
            txtten.Text = dv["tensv"].ToString();
            datePicker1.Text = dv["ngaysinh"].ToString();
            txtgioitinh.Text = dv["gioitinh"].ToString();
            txtkhoa.Text = dv["makhoa"].ToString();
            btnluu.IsEnabled = true;
            btnhuy.IsEnabled = true;

        }
    }
}
